<?php

namespace app\admin\controller;

use app\common\model\Domains;
use app\common\model\Dsps;
use app\common\model\Qrcodes;
use think\Request;

class Qrcode extends Base
{

    /* 获取入口链接 */
    public function getrukouyuming($alias=0)
    {
        $url = fission('https') . '://' . Domains::getOneData(1) . url('item/dsp/index', ['alias'=>$alias]);
        success($url);
    }


    public function index(Request $request)
    {
        if ( $request->isAjax() ){

            $page = input('param.page/d', 1);
            $limit = input('param.limit/d', 20);

            $data = array(
                'data'      =>  Qrcodes::order(['id'=>'desc'])->page($page)->limit($limit)->select(),
                'count'     =>  Qrcodes::count('id'),
                'code'      => 0,
                'msg'       => '',
            );

            return json($data);
        }

        return view();
    }


    public function create(Request $request)
    {
        if ( $request->isPost() ){
            $post = trimAll($request->post());

            error('演示网站, 不允许修改, 客服:kk71105');

            $code = new \dh2y\qrcode\QRcode();
            $code_path = $code->png($post['url'], false, $post['size'])
            ->background($post['bgx'], $post['bgy'], $post['bg'])
            ->text($post['font'], $post['fontsize'],[$post['fontx']+2, $post['fonty']+2], '#eeeeee')
            ->text($post['font'], $post['fontsize'],[$post['fontx'], $post['fonty']], $post['fontcolor'])
            ->getPath();

            if ( $post['xuanze'] ){
                success('生成海报 成功', 3, $code_path);
            }

            $data['alias'] = $post['alias'];
            $data['url'] = $post['url'] ? $post['url'] : 'No url';
            $data['img'] = $code_path;
            $data['status'] = isset($post['status']) ? $post['status'] : 2;

            //保存到数据库
            if ( Qrcodes::create($data) ){
                createLog('生成短视频海报 成功：' . $data['alias']);
                success('保存成功');
            }else{
                createLog('生成短视频海报 失败：' . $data['alias']);
                error('保存失败');
            }
        }

        $dsps = Dsps::field('alias,share_title')->where(['status'=>1])->order('id', 'desc')->select();
        $this->assign('dsps', $dsps);
        return view();
    }


    public function edit(Request $request, $id)
    {

        if ( $request->isPost() ){
            $post = trimAll($request->post());

            error('演示网站, 不允许修改, 客服:kk71105');

            $post['status'] = isset($post['status']) ? $post['status'] : 2;
            unset($post['file']);

            //保存到数据库
            if ( Qrcodes::where('id',$id)->update($post) ){
                success('保存成功');
            }else{
                error('保存失败');
            }

        }

        $data = Qrcodes::get($id);
        $this->assign('data', $data);

        $dsps = Dsps::field('alias,share_title')->where(['status'=>1])->order('id', 'desc')->select();
        $this->assign('dsps', $dsps);

        return view();
    }

























}
